﻿using Framework.Domain.Enum;
using Framework.Domain.Model;
using Framework.Infrastructure.Cache.Redis;
using Framework.MVC.Infrastructure;
using Framework.MVC.Infrastructure.Filters;
using Framework.Repository;
using Framework.Repository.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace Framework.MVC.Controllers
{
    public class BackUserController : BaseController
    {
        /*
         * 所有涉及到用户实体的变化（更改密码、更改组织机构等）都需要干掉token让用户重新登录
         */

        SYS_BackUserService BackUserService = new SYS_BackUserService();

        [HttpGet]
        [BKTokenValidate]
        public JsonResult GetModel(long id)
        {
            var model = BackUserService.FindByKey(id);
            return Json(new ReturnModel_Data() { data = model });
        }

        [HttpGet]
        [BKTokenValidate]
        [BKSystemLog(Category = CategoryEnum.系统管理, Action = ActionEnum.后台用户, Operate = OperateEnum.后台用户_查询)]
        public JsonResult GetListPaged(QueryCondition condition)
        {
            var list = BackUserService.FindPaged(condition, null);
            return Json(new ReturnModel_BootstrapTable_Pager { total = list.TotalCount, rows = list.ToList() });
        }

        [HttpPost]
        [BKTokenValidate]
        [BKSystemLog(Category = CategoryEnum.系统管理, Action = ActionEnum.后台用户, Operate = OperateEnum.后台用户_单个保存)]
        public JsonResult SaveModel(SYS_BackUser model, BKTokenModel tokenModel)
        {
            /*
             * 用户名不能重复（加锁、主键约束）
             * 
             */

            var result = BackUserService.SaveModel(model, tokenModel);
            ReturnModel_Common returnModel = new ReturnModel_Common();
            switch (result)
            {
                case 0:
                    //成功
                    break;
                case 1:
                    returnModel.success = false;
                    returnModel.code = ReturnCode_System.操作失败;
                    returnModel.msg = "USERNAMES_LENGTH_SHOULD_BE_BETWEEN_0_AND_64";
                    break;
                case 2:
                    returnModel.success = false;
                    returnModel.code = ReturnCode_System.操作失败;
                    returnModel.msg = "USERPWDS_LENGTH_SHOULD_BE_BETWEEN_0_AND_64";
                    break;
                case 3:
                    returnModel.success = false;
                    returnModel.code = ReturnCode_System.已经存在此账号;
                    returnModel.msg = "USERNAME_ALREADY_EXISTS";
                    break;
                case 4:
                    returnModel.success = false;
                    returnModel.code = ReturnCode_System.找不到当前用户;
                    returnModel.msg = "CURRENT_USER_ALREADY_DELETED";
                    break;
                default:
                    returnModel.success = false;
                    returnModel.code = ReturnCode_System.操作失败;
                    returnModel.msg = "UNKNOWN_ERROR";
                    break;
            }
            return Json(returnModel);
        }

        [HttpPost]
        [BKTokenValidate]
        [BKSystemLog(Category = CategoryEnum.系统管理, Action = ActionEnum.后台用户, Operate = OperateEnum.后台用户_单个删除)]
        public JsonResult DeleteModel(long id)
        {
            /*
             * 级联删除 手动删除级联表数据
             * 
             */
            int i = BackUserService.DeleteModel(id);
            if (i > 0)
            {
                return Json(new ReturnModel_Common() { });
            }
            else
            {
                return Json(new ReturnModel_Common { success = false, code = ReturnCode_System.操作失败, msg = "FAIL" });
            }
        }

        [HttpPost]
        [BKTokenValidate]
        [BKSystemLog(Category = CategoryEnum.系统管理, Action = ActionEnum.后台用户, Operate = OperateEnum.后台用户_批量删除)]
        public JsonResult DeleteList(long[] ids)
        {
            /*
             * 级联删除 手动删除级联表数据
             * 
             */



            int i = BackUserService.DeleteList(ids);
            return Json(new ReturnModel_Data() { data = i });
        }



        /*******************************/

        public JsonResult UserDepartments(long id)
        {
            var model = BackUserService.LoadUserDepartments(id);
            return Json(new ReturnModel_Data() { data = model });
        }

        public JsonResult DepartmentUsers(int id)
        {
            var model = BackUserService.LoadDepartmentUsers(id);
            return Json(new ReturnModel_Data() { data = model });
        }

    }
}